using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
namespace MicroMsg.UI.UserContrl
{
	public class PostPanoramaContainer : UserControl
	{
		private enum SlideType
		{
			SlideToRight,
			SlideToLeft
		}
		private const string TAG = "PostPanoramaContainer";
		public SnsPostPage postMainPage;
		public PostPreViewImage[] mImage = new PostPreViewImage[3];
		public int nowIndex;
		private bool bIfInited;
		private int pOldIndex = 1;
		private PostPanoramaContainer.SlideType mSlideType;
		private bool bSlideWithDel;
		internal Grid LayoutRoot;
		internal Panorama panoramaCtrl;
		internal PanoramaItem pItem1;
		internal Grid Grid1;
		internal PanoramaItem pItem2;
		internal Grid Grid2;
		internal PanoramaItem pItem3;
		internal Grid Grid3;
		private bool _contentLoaded;
		public Image CurrentImgCtrl
		{
			get
			{
				return this.mImage[this.panoramaCtrl.SelectedIndex].ShowImg;
			}
		}
		public bool bCurrentImgScaled
		{
			get
			{
				return this.mImage[this.panoramaCtrl.SelectedIndex].transform.get_ScaleX() != 1.0;
			}
		}
		public PostPanoramaContainer()
		{
			this.InitializeComponent();
			this.panoramaCtrl.set_Visibility(1);
			this.panoramaCtrl.DefaultItem = this.panoramaCtrl.get_Items().get_Item(1);
			base.add_Loaded(new RoutedEventHandler(this.PostPanoramaContainer_Loaded));
		}
		private void PostPanoramaContainer_Loaded(object sender, RoutedEventArgs e)
		{
			base.get_Dispatcher().BeginInvoke(delegate
			{
				this.panoramaCtrl.set_Visibility(0);
			});
		}
		public void InitData(int nowIdx)
		{
			if (!this.bIfInited)
			{
				BasePage prevPage = PageManager.getInstance().GetPrevPage();
				if (prevPage is SnsPostPage)
				{
					this.postMainPage = (prevPage as SnsPostPage);
				}
				this.nowIndex = nowIdx;
				this.InitSlideImage();
				this.bIfInited = true;
			}
		}
		private void SetImageSource(PostPreViewImage imgCtrl, int index)
		{
			if (this.postMainPage == null)
			{
				return;
			}
			if (this.postMainPage.picPathList.get_Item(index).mPicture == null)
			{
				imgCtrl.InitView(ImageCacheMgr.loadFromFile(this.postMainPage.picPathList.get_Item(index).strUrl), this.postMainPage.picPathList.get_Item(index).nAngle);
				return;
			}
			BitmapImage bitmapImage = new BitmapImage();
			bitmapImage.SetSource(this.postMainPage.picPathList.get_Item(index).mPicture.GetImage());
			imgCtrl.InitView(bitmapImage, this.postMainPage.picPathList.get_Item(index).nAngle);
		}
		private void ClearImageSource(PostPreViewImage imgCtrl)
		{
			imgCtrl.ClearImage();
		}
		private void InitSlideImage()
		{
			if (this.postMainPage == null)
			{
				return;
			}
			this.mImage[0] = new PostPreViewImage();
			this.mImage[1] = new PostPreViewImage();
			this.mImage[2] = new PostPreViewImage();
			this.Grid1.get_Children().Add(this.mImage[0]);
			this.Grid2.get_Children().Add(this.mImage[1]);
			this.Grid3.get_Children().Add(this.mImage[2]);
			this.SetImageSource(this.mImage[1], this.nowIndex);
			if (this.nowIndex > 0)
			{
				this.SetImageSource(this.mImage[0], this.nowIndex - 1);
			}
			else
			{
				this.ClearImageSource(this.mImage[0]);
			}
			if (this.nowIndex + 1 < this.postMainPage.picPathList.get_Count())
			{
				this.SetImageSource(this.mImage[2], this.nowIndex + 1);
				return;
			}
			this.ClearImageSource(this.mImage[2]);
		}
		private void panoramaCtrl_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
		{
			if (this.bCurrentImgScaled)
			{
				e.set_Handled(true);
				e.Complete();
			}
		}
		private void panoramaCtrl_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
		{
			if (this.bCurrentImgScaled)
			{
				e.set_Handled(true);
				e.Complete();
			}
			if (this.postMainPage == null)
			{
				return;
			}
			if (this.nowIndex == 0 && e.get_DeltaManipulation().get_Translation().get_X() > 0.0)
			{
				e.set_Handled(true);
				e.Complete();
				return;
			}
			if (this.nowIndex == this.postMainPage.picPathList.get_Count() - 1 && e.get_DeltaManipulation().get_Translation().get_X() < 0.0)
			{
				e.set_Handled(true);
				e.Complete();
			}
		}
		private void panoramaCtrl_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
		{
		}
		private void panoramaCtrl_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			if (this.postMainPage == null)
			{
				return;
			}
			int selectedIndex = this.panoramaCtrl.SelectedIndex;
			if (selectedIndex == this.pOldIndex)
			{
				return;
			}
			if ((selectedIndex - this.pOldIndex + 3) % 3 == 1)
			{
				this.mSlideType = PostPanoramaContainer.SlideType.SlideToRight;
			}
			else
			{
				this.mSlideType = PostPanoramaContainer.SlideType.SlideToLeft;
			}
			if ((this.mSlideType == PostPanoramaContainer.SlideType.SlideToLeft && this.nowIndex == 0) || (this.mSlideType == PostPanoramaContainer.SlideType.SlideToRight && this.nowIndex == this.postMainPage.picPathList.get_Count() - 1))
			{
				base.get_Dispatcher().BeginInvoke(delegate
				{
					this.panoramaCtrl.DefaultItem = this.panoramaCtrl.get_Items().get_Item(this.pOldIndex);
				});
				return;
			}
			this.pOldIndex = selectedIndex;
			base.get_Dispatcher().BeginInvoke(delegate
			{
				this.RefreshNowIndexImg(this.mSlideType);
				this.LoadNearbyImg(this.mSlideType);
			});
		}
		private void RefreshNowIndexImg(PostPanoramaContainer.SlideType sType)
		{
			if (this.postMainPage == null)
			{
				return;
			}
			if (sType == PostPanoramaContainer.SlideType.SlideToLeft)
			{
				if (this.nowIndex > 0)
				{
					this.nowIndex--;
				}
				return;
			}
			if (this.bSlideWithDel)
			{
				return;
			}
			if (this.nowIndex + 1 >= this.postMainPage.picPathList.get_Count())
			{
				return;
			}
			this.nowIndex++;
		}
		private void LoadNearbyImg(PostPanoramaContainer.SlideType sType)
		{
			if (this.postMainPage == null)
			{
				return;
			}
			if (this.bSlideWithDel)
			{
				if (sType == PostPanoramaContainer.SlideType.SlideToLeft)
				{
					this.postMainPage.DelSnsMediaInUI(this.nowIndex + 1);
				}
				else
				{
					this.postMainPage.DelSnsMediaInUI(this.nowIndex);
				}
			}
			if (sType == PostPanoramaContainer.SlideType.SlideToLeft || this.bSlideWithDel)
			{
				if (this.nowIndex > 0)
				{
					this.SetImageSource(this.mImage[(this.panoramaCtrl.SelectedIndex + 2) % 3], this.nowIndex - 1);
				}
				else
				{
					this.ClearImageSource(this.mImage[(this.panoramaCtrl.SelectedIndex + 2) % 3]);
				}
			}
			if (sType == PostPanoramaContainer.SlideType.SlideToRight || this.bSlideWithDel)
			{
				if (this.nowIndex < this.postMainPage.picPathList.get_Count() - 1)
				{
					this.SetImageSource(this.mImage[(this.panoramaCtrl.SelectedIndex + 1) % 3], this.nowIndex + 1);
				}
				else
				{
					this.ClearImageSource(this.mImage[(this.panoramaCtrl.SelectedIndex + 1) % 3]);
				}
			}
			this.bSlideWithDel = false;
		}
		public void RemoveUISnsMediaIndex()
		{
			if (this.nowIndex < 0 || this.nowIndex >= this.postMainPage.picPathList.get_Count())
			{
				Log.smethod_1("PostPanoramaContainer", "Index error: nowIndex = " + this.nowIndex);
				return;
			}
			if (this.nowIndex != 0)
			{
				this.bSlideWithDel = true;
				this.GoPrevious();
				return;
			}
			if (this.postMainPage.picPathList.get_Count() == 1)
			{
				this.postMainPage.DelSnsMediaInUI(0);
				ToolFunction.GetRootPage().GoBack();
				return;
			}
			this.bSlideWithDel = true;
			this.GoNext();
		}
		public void GoPrevious()
		{
			if (this.nowIndex - 1 < 0)
			{
				Log.smethod_1("PostPanoramaContainer", "Index error: nowIndex - 1 = " + (this.nowIndex - 1).ToString());
				return;
			}
			int num = (this.panoramaCtrl.SelectedIndex + 2) % 3;
			this.panoramaCtrl.DefaultItem = this.panoramaCtrl.get_Items().get_Item(num);
		}
		public void GoNext()
		{
			if (this.nowIndex + 1 >= this.postMainPage.picPathList.get_Count())
			{
				Log.smethod_1("PostPanoramaContainer", "Index error: nowIndex + 1 = " + (this.nowIndex + 1).ToString() + " , postMainPage.picPathList.Count = " + this.postMainPage.picPathList.get_Count().ToString());
				return;
			}
			int num = (this.panoramaCtrl.SelectedIndex + 1) % 3;
			this.panoramaCtrl.DefaultItem = this.panoramaCtrl.get_Items().get_Item(num);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Common/UI/UploadAndPost/PostPanoramaContainer.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.panoramaCtrl = (Panorama)base.FindName("panoramaCtrl");
			this.pItem1 = (PanoramaItem)base.FindName("pItem1");
			this.Grid1 = (Grid)base.FindName("Grid1");
			this.pItem2 = (PanoramaItem)base.FindName("pItem2");
			this.Grid2 = (Grid)base.FindName("Grid2");
			this.pItem3 = (PanoramaItem)base.FindName("pItem3");
			this.Grid3 = (Grid)base.FindName("Grid3");
		}
	}
}
